﻿//----------------------------------------------------------------
// <copyright file="MptEventLoggerTest.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//----------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Security;
using MptCodeLibrary.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Test.UnitTest
{
   
    [TestClass]
    public class MptEventLoggerTest : UnitTestBase
    {
        private const string source = "Microsoft Process Template";
        private const string log = "Application";

        [TestInitialize]
        public void TestInitialize()
        {
            try
            {
                EventLog.SourceExists(source);
            }
            catch (SecurityException)
            {
                Assert.IsFalse(true, "Can't run the Event Log tests unless VS is running with administrator privileges.");
            }
        }

        [TestMethod]
        public void WriteTraceCreatesEventSource()
        {
            if (EventLog.SourceExists(source))
                EventLog.DeleteEventSource(source);

            MptEventLogger.WriteTrace(string.Empty);

            Assert.IsTrue(EventLog.SourceExists(source));
        }

        [TestMethod]
        public void VerifyWriteTrace()
        {
            string traceMessage = "Test trace at: " + DateTime.Now;
            MptEventLogger.WriteTrace(traceMessage);

            Assert.IsTrue(IsMessageInEventLog(traceMessage, EventLogEntryType.Information),
                          "The event is in the event log");
        }

        [TestMethod]
        public void WriteErrorCreatesEventSource()
        {
            if (EventLog.SourceExists(source))
                EventLog.DeleteEventSource(source);
            
            MptEventLogger.WriteError(string.Empty);

            Assert.IsTrue(EventLog.SourceExists(source));
        }

        [TestMethod]
        public void VerifyWriteError()
        {
            string traceMessage = "Test trace at: " + DateTime.Now;
            MptEventLogger.WriteError(traceMessage);

            Assert.IsTrue(IsMessageInEventLog(traceMessage, EventLogEntryType.Error),
                          "The event is in the event log");

        }

        private static bool IsMessageInEventLog(string message, EventLogEntryType eventLogEntryType)
        {
            bool foundEvent = false;
            EventLog appLog = new EventLog(log, Environment.MachineName, source);
            for (int index = 0; index < appLog.Entries.Count; ++index)
            {
                if (appLog.Entries[index].Message == message
                    && appLog.Entries[index].EntryType == eventLogEntryType)
                {
                    foundEvent = true;
                    break;
                }
            }
            return foundEvent;
        }
    }
}
